﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UtilZ.Dotnet.Ex.Model;

namespace ConsoleApp.Test
{
    unsafe internal class TestUnmanagedMemoryPointer
    {
        public static void Test()
        {
            //TestUnmanagedMemoryPointer2();
            //TestIQUnmanagedMemoryPointer();
            TestDebugView();
        }

        private static void TestDebugView()
        {
            var pointer = new UnmanagedMemoryPointer<int>(10);
            var pointer2 = new IQUnmanagedMemoryPointer<int>(10);
            for (int i = 0; i < 10; i++)
            {
                pointer[i] = i;
                pointer2.SetI(i, i + 1);
                pointer2.SetQ(i, i + 10);
            }


            pointer.Dispose();
            pointer2.Dispose();
        }

        private static void TestIQUnmanagedMemoryPointer()
        {
            int count = 4;
            var ptrItem = new IQUnmanagedMemoryPointer(sizeof(short));

            ptrItem.Check(count);
            short* shortIDataPtr = (short*)ptrItem.IDataPtr;
            short* shortQDataPtr = (short*)ptrItem.QDataPtr;

            for (int i = 0; i < count; i++)
            {
                *shortIDataPtr = (short)(i + 1);
                shortIDataPtr++;

                *shortQDataPtr = (short)(i * 2 + 1);
                shortQDataPtr++;
            }

            shortIDataPtr = (short*)ptrItem.IDataPtr;
            shortQDataPtr = (short*)ptrItem.QDataPtr;
            for (int i = 0; i < count; i++)
            {
                Console.WriteLine(string.Format("{0}    {1} {2}", i + 1, *shortIDataPtr, *shortQDataPtr));
                shortIDataPtr++;
                shortQDataPtr++;
            }

            Console.WriteLine("-----------------");
            count = 6;
            ptrItem.Check(count);
            shortIDataPtr = (short*)ptrItem.IDataPtr;
            shortQDataPtr = (short*)ptrItem.QDataPtr;
            for (int i = 0; i < count; i++)
            {
                *shortIDataPtr = (short)((i + 1) * 10);
                shortIDataPtr++;

                *shortQDataPtr = (short)((i * 2 + 1) * 10);
                shortQDataPtr++;
            }

            OutputIQSpanZ(ptrItem.GetIQSpanZ<short>());

            //shortIDataPtr = (short*)ptrItem.IDataPtr;
            //shortQDataPtr = (short*)ptrItem.QDataPtr;
            //for (int i = 0; i < count; i++)
            //{
            //    Console.WriteLine(string.Format("{0}    {1} {2}", i + 1, *shortIDataPtr, *shortQDataPtr));
            //    shortIDataPtr++;
            //    shortQDataPtr++;
            //}

            ptrItem.Dispose();

        }

        private static void OutputIQSpanZ<T>(IQSpanZ<T> span) where T : struct
        {
            Console.WriteLine("OutputIQSpanZ");
            for (int i = 0; i < span.Length; i++)
            {
                Console.WriteLine(string.Format("{0}    {1} {2}", i + 1, span.GetI(i), span.GetQ(i)));
            }
            Console.WriteLine("-----------------");
        }

        private static void TestUnmanagedMemoryPointer2()
        {
            int count = 4;
            var ptrItem = new UnmanagedMemoryPointer(sizeof(short));

            ptrItem.Check(count);
            short* shortPtr = (short*)ptrItem.DataPtr;
            for (int i = 0; i < count; i++)
            {
                *shortPtr = (short)(i + 1);
                shortPtr++;
            }

            shortPtr = (short*)ptrItem.DataPtr;
            for (int i = 0; i < count; i++)
            {
                Console.WriteLine(string.Format("{0}    {1}", i + 1, *shortPtr));
                shortPtr++;
            }

            Console.WriteLine("-----------------");
            count = 6;
            ptrItem.Check(count);

            shortPtr = (short*)ptrItem.DataPtr;
            for (int i = 0; i < count; i++)
            {
                *shortPtr = (short)((i + 1) * 10);
                shortPtr++;
            }

            OutputSpanZ(ptrItem.GetSpanZ<short>());

            //shortPtr = (short*)ptrItem.DataPtr;
            //for (int i = 0; i < count; i++)
            //{
            //    Console.WriteLine(string.Format("{0}    {1}", i + 1, *shortPtr));
            //    shortPtr++;
            //}

            ptrItem.Dispose();
        }



        private static void OutputSpanZ<T>(SpanZ<T> span) where T : struct
        {
            Console.WriteLine("OutputSpanZ");
            for (int i = 0; i < span.Length; i++)
            {
                Console.WriteLine(string.Format("{0}    {1}", i + 1, span[i]));
            }
            Console.WriteLine("-----------------");
        }
    }
}
